library(tidyverse)


# loading data
source("../load_data.R")

generate_features <- function(data) {
  # 计算每个学生在每个题目上的时间花费
  time_spent <- data %>%
    group_by(STUDENTID, AccessionNumber) %>%
    summarize(
      EnterTime = EventTime[Observable == "Enter Item"],
      ExitTime = EventTime[Observable == "Exit Item"],
      TimeSpent = as.numeric(difftime(ExitTime, EnterTime, units = "secs"))
    ) %>%
    ungroup()
  
  # 计算每个学生的响应时间
  response_times <- time_spent %>%
    group_by(STUDENTID, AccessionNumber) %>%
    summarise(ResponseTime = sum(TimeSpent)) %>%
    mutate(ResponseTime = as.numeric(ResponseTime))
  
  # 计算每个学生的平均响应时间和响应时间的波动性
  student_stats <- response_times %>%
    group_by(STUDENTID) %>%
    summarise(
      AverageResponseTime = mean(ResponseTime, na.rm = TRUE),
      ResponseTimeSD = sd(ResponseTime, na.rm = TRUE)
    )
  
  # 计算每个题目的 5% 分位数
  percentile_5 <- response_times %>%
    group_by(AccessionNumber) %>%
    summarise(Percentile5 = quantile(ResponseTime, probs = 0.05, na.rm = TRUE), .groups = 'drop')
  
  # 标记每个学生每个题目的响应时间是否低于 5% 分位数
  response_times <- response_times %>%
    left_join(percentile_5, by = "AccessionNumber") %>%
    mutate(IsExceptional = as.integer(ResponseTime < Percentile5))
  
  # 统计每个学生的异常题目数量
  exceptional_counts <- response_times %>%
    group_by(STUDENTID) %>%
    summarise(TotalExceptions = sum(IsExceptional, na.rm = TRUE), .groups = 'drop')
  
  # 合并结果并返回
  final_stats <- student_stats %>%
    left_join(exceptional_counts, by = "STUDENTID")
  
  return(final_stats)
}

test_features_restime <- generate_features(testBlockA) 

train_features_restime <- generate_features(trainBlockA) 


save(test_features_restime,train_features_restime,
     file = "features_restime.Rdata")
